package com.bdqn.vueboot.controller;

import com.bdqn.vueboot.entity.User;
import com.bdqn.vueboot.service.UserService;
import com.bdqn.vueboot.util.ResultStateEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @ApiOperation(value = "查询用户列表")
    @CrossOrigin
    @RequestMapping(value = "/list")
    public Object list(@RequestParam(value = "pageIndex", required = false, defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "usrName", required = false, defaultValue = "") String usrName,
                       @RequestParam(value = "limit", required = false, defaultValue = "5") Integer limit,
                       Model model) {

        Map<String, String> map = new HashMap<>();
        map.put("usrName", usrName);
        Sort sort = new Sort(Sort.Direction.ASC, "usrId");
        Pageable pageable = PageRequest.of(pageIndex - 1, limit, sort);
        Page<User> page = userService.findPageByMap(map, pageable);

        //model.addAttribute("userPager", page);
        //model.addAttribute("usrName", usrName);
        return page;
    }

    @CrossOrigin
    //@RequestMapping(value = "/add")
    @ApiOperation(value = "添加用户")
    @GetMapping(value = "/add")
    public Object add(
            @RequestParam(value = "usrName", required = true) String usrName) {
        User user = new User();
        user.setUsrName(usrName);
        try {
            userService.saveUser(user);
            return ResultStateEnum.SUCCESS.getResultState();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultStateEnum.ERROR.getResultState();
        }
    }

    @ApiOperation(value = "根据ID删除")
    @CrossOrigin
    @RequestMapping(value = "/deleteById")
    public Object deleteById(@RequestParam(value = "id", required = true) Long id) {
        try {
            userService.deleteByUserId(id);
            return ResultStateEnum.SUCCESS.getResultState();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultStateEnum.ERROR.getResultState();
        }
    }

    @ApiOperation(value = "根据ID修改")
    @CrossOrigin
    @RequestMapping(value = "/update")
    public Object update(@RequestParam(value = "id", required = true) Long id,
                         @RequestParam(value = "usrName", required = true) String usrName) {
        User user = new User();
        user.setUsrId(id);
        user.setUsrName(usrName);
        try {
            userService.saveUser(user);
            return ResultStateEnum.SUCCESS.getResultState();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultStateEnum.ERROR.getResultState();
        }
    }
}
